package com.peakmain.baselibrary.db;

import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import java.io.File;

/**
 * 作者：Peakmain
 * 版本：1.0
 * 创建日期：2018/6/23 0023 上午 10:26
 * 邮件:2726449200@qq.com
 * 描述：
 */
public class DaoSupportFactory {
    private static volatile DaoSupportFactory mFactory;
    //持有数据库的引用
    private final SQLiteDatabase mSqLiteDatabase;

    private DaoSupportFactory() {
        //把数据库放到内存卡中
        File dbRoot = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath() + File.separator + "project" + File.separator + "database");
        if (!dbRoot.exists()) {
            dbRoot.mkdirs();
        }
        File dbFile = new File(dbRoot, "database.db");
        //打开或者创建数据库
        mSqLiteDatabase = SQLiteDatabase.openOrCreateDatabase(dbFile, null);

    }

    public static DaoSupportFactory getInstance() {
        if (mFactory == null) {
            synchronized (DaoSupportFactory.class) {
                if (mFactory == null) {
                    mFactory = new DaoSupportFactory();
                }
            }
        }
        return mFactory;
    }

    public <T> IDaoSupport<T> getDao(Class<T> clazz) {
        IDaoSupport<T> daoSoupport = new DaoSupport();
        daoSoupport.init(mSqLiteDatabase, clazz);
        return daoSoupport;
    }
}
